﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data;
using System.IO;
using System.Xml;

using Museum.DBAccess;
using System.Text;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]

public class MuseumService : System.Web.Services.WebService
{
    [WebMethod(Description = "GetMuseumByCity gives Museums by passing City name (e.g. New York, Paris)")]
    public XmlElement GetMuseumByCity(string cityName)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(cityName))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForMuseumByCity(cityName));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "GetGames gives Games by passing MuseumId (e.g., 1,2,3)")]
    public XmlElement GetGames(int MuseumId)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(MuseumId.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForGameByMuseum(MuseumId));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "DownloadGame gives game data by passing gameId (e.g., 1,2,3) and isFirstTimeDownload (e.g. 0,1)")]
    public XmlElement DownloadGame(int gameId, int isFirstTimeDownload, int requestingDevice)
    {
        // Response should be bool which will return only change is version.
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(gameId.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForGameByGameId(gameId, isFirstTimeDownload, requestingDevice));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "CheckForUpdate gives game update data by passing gameId (e.g., 1,2,3) and version (e.g., 1,2,3)")]
    public XmlElement CheckForUpdate(int gameId, int version)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(gameId.ToString()) || string.IsNullOrEmpty(version.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForGameUpdate(gameId, version));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "SubmitReview submits user's review by passing GameId (e.g., 1,2,3) and ReveiwComments (e.g., 'User Comments') and UserName(e.g., 'A','B') and Rating(e.g, 1,2,3)")]
    public XmlElement SubmitReview(int GameID, string ReviewComments, string Username, short Rating)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(GameID.ToString()) || string.IsNullOrEmpty(ReviewComments) || string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Rating.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForSubmitReview(GameID, ReviewComments, Username, Rating));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "GetTopScore gives top scores by passing count(e.g., 1,2,3) and gameId (e.g., 1,2,3)")]
    public XmlElement GetTopScore(int count, int gameId)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(count.ToString()) || string.IsNullOrEmpty(gameId.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForGameTopScore(count, gameId));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "SubmitScore submits score details by passing gameId(e.g., 1,2,3) and score(e.g., 10,20,30) and userName(e.g., 'A','B','C')")]
    public XmlElement SubmitScore(int gameId, string userName, int score)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(gameId.ToString()) || string.IsNullOrEmpty(userName.ToString()) || string.IsNullOrEmpty(score.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForSubmitGameScore(gameId, userName, score));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "GetCategories gives a list of all categories by passing MuseumId(e.g. 1,2,3).")]
    public XmlElement GetCategories(int MuseumId)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(MuseumId.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForCategories(MuseumId));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "GetReview gives a list of all reviews for a user by passing GameId(e.g., 1,2,3).")]
    public XmlElement GetReview(int gameId)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(gameId.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForReviews(gameId));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "GetTourData gives tour details by passing museumId (e.g., 1,2,3) and fromIndex(e.g., 0,10,20) and pageSize(e.g., 10,20,30)")]
    public XmlElement GetTourData(int museumId, int fromIndex, int pageSize)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(museumId.ToString()) || string.IsNullOrEmpty(fromIndex.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForTourData(museumId, fromIndex, pageSize));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "GetTreasureDetails gives Treasure Details by passing treasureId (e.g., 1,2,3)")]
    public XmlElement GetTreasureDetails(int treasureId)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(treasureId.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForTreasureDetails(treasureId));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "CheckVoucher validates voucher by passing Voucher number (e.g., ABC1123,ABD1213,ACC1123) and gameId (e.g. 1,2)")]
    public XmlElement CheckVoucher(string voucher, int gameId)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(voucher))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForVoucherStatus(voucher, gameId));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "SearchArt searches arts by passing searchString (e.g., 'any string') and searchColumn like floor, gallery etc.. and CategoryId(e.g., 1,2,3) and MuseumId(e.g., 1,2,3) and fromIndex(e.g., 0,10,20) and pageSize(e.g., 10,20,30)")]
    public XmlElement SearchArt(string searchString, string searchInColumnName, int categoryId, int museumId, int fromIndex, int pageSize)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(searchString) || string.IsNullOrEmpty(searchInColumnName) || string.IsNullOrEmpty(categoryId.ToString()) || string.IsNullOrEmpty(museumId.ToString()) || string.IsNullOrEmpty(fromIndex.ToString()) || string.IsNullOrEmpty(pageSize.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForSearchArt(searchString, searchInColumnName, categoryId, museumId, fromIndex, pageSize));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "GetArts gives all arts by passing CategoryId (e.g., 1,2,3) and fromIndex(e.g., 0,10,20) and pageSize(e.g., 10,20,30)")]
    public XmlElement GetArts(int categoryId, int fromIndex, int pageSize)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(categoryId.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForGetArts(categoryId, fromIndex, pageSize));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "GetCity gives all cities by passing CountryId (e.g., 1,2,3) and UpdatedDate(e.g., YYYY-MM-DD) ")]
    public XmlElement GetCity(int countryId, string updatedDate)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            try
            {
                DateTime cityUpdated = Convert.ToDateTime(updatedDate);

                if (string.IsNullOrEmpty(countryId.ToString()) || string.IsNullOrEmpty(updatedDate.ToString()))
                {
                    xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
                }
                else
                    if (Convert.ToDateTime(updatedDate).GetType() != DateTime.Now.GetType()) { }
                    else
                        xmlDoc.LoadXml(XMLResponse.ForGetCity(countryId, cityUpdated));
            }
            catch
            {
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

    [WebMethod(Description = "GetArtDetails gives art details by passing artId (e.g., 1,2,3) ")]
    public XmlElement GetArtDetails(int artId)
    {
        XmlDocument xmlDoc = null;
        try
        {
            xmlDoc = new XmlDocument();
            if (string.IsNullOrEmpty(artId.ToString()))
                xmlDoc.LoadXml(XMLResponse.ForInvalidInputParameter());
            else
                xmlDoc.LoadXml(XMLResponse.ForGetArtDetails(artId));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return xmlDoc.DocumentElement;
    }

}